<template>
  <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <!-- 准备三个模板 -->
      <!-- 无图模式 -->
      <div class="title-box" v-if="articleData.cover.type === 0">
        <!-- 标题 -->
        <span>{{ articleData.title }}</span>
      </div>

      <!-- 单图模式 -->
      <div class="title-box" v-if="articleData.cover.type === 1">
        <!-- 标题 -->
        <span>{{ articleData.title }}</span>
        <!-- 单图 -->
        <img v-lazy="articleData.cover.images[0]" class="thumb" />
        <!-- <van-image
          class="thumb"
          width="113"
          height="70"
          lazy-load
          :src="articleData.cover.images[0]"
        /> -->
      </div>

      <!-- 三图模式 -->
      <div  class="thumb-box" v-if="articleData.cover.type === 3">
        <img
          v-for="(url, index) in articleData.cover.images"
          :key="index"
          class="thumb"
          v-lazy="url"
        />
        <!-- <van-image
          v-for="(url, index) in articleData.cover.images"
          :key="index"
          class="thumb"
          width="113"
          height="70"
          lazy-load
          :src="url"
        /> -->
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{ articleData.aut_name }}</span>
          <span>{{ articleData.comm_count }}评论</span>
          <!-- 差值支持函数调用 渲染的结果是当前函数的返回值 -->
          <span>{{ formatTime(articleData.pubdate) }}</span>
        </div>
      </div>
    </template>
  </van-cell>
</template>

<script>
import { formatRelativeTime } from '@/utils'
export default {
  name: 'article-list-item',
  props: {
    articleData: {
      // 这里一定要采取对象的写法 指定类型 如果是引用类型的数据
      // 设置默认值 需要采取工厂函数的写法 以返回值的格式编写默认数据
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  methods: {
    formatTime (time) {
      return formatRelativeTime(time)
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
